﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using Newtonsoft.Json;

namespace BrightcoveAPI
{   
    // <summary>
    // The Video object is an aggregation of metadata and asset information associated with a video
    // </summary>
    public class BCVideo : BCObject
    {
        #region Private Properties

        private long _id;
        private long _accountId;
        private DateTime _creationDate;
        private DateTime _publishedDate;
        private DateTime _lastModifiedDate;
        private DateTime _startDate;
        private DateTime _endDate;
        private long _length;
        private int _playsTotal;
        private int _playsTrailingWeek;
        private String _FLVURL;

        #endregion Private Properties

        #region Public Properties

        // <summary>
        // The title of this Video.
        // </summary> 
        public String name { get; set; }

        // <summary>
        // A number that uniquely identifies this Video, assigned by Brightcove when the Video is created.
        // </summary>
        public long id { get { return _id; } }

        // <summary>
        // A user-specified id that uniquely identifies this Video. ReferenceID can be used as a foreign-key to identify this video in another system. 
        // </summary> 
        public String referenceId { get; set; }

        // <summary>
        // A number, assigned by Brightcove, that uniquely identifies the account to which this Video belongs.
        // </summary> 
        public long accountId { get { return _accountId; } }

        // <summary>
        // A short description describing this Video, limited to 256 characters.
        // </summary> 
        public String shortDescription { get; set; }

        // <summary>
        // A longer description of this Video, bounded by a 5000 character limit.
        // </summary> 
        public String longDescription { get; set; }

        // <summary>
        // The URL of the video file for this Video. Note that this property can be accessed with the Media API 
        // only with a special read or write token. See Accessing Video Content with the Media API.
        // </summary> 
        public String FLVURL { get{ return _FLVURL;} }

        // <summary>
        // A List of Renditions that represent the dynamic delivery renditions available for this Video.
        // </summary> 
        public List<BCRendition> renditions = new List<BCRendition>();

        // <summary>
        // The date this Video was created
        // </summary> 
        public DateTime creationDate { get { return _creationDate; } }

        // <summary>
        // The date this Video was last made active
        // </summary> 
        public DateTime publishedDate { get { return _publishedDate; } }

        // <summary>
        // The date this Video was last modified
        // </summary> 
        public DateTime lastModifiedDate { get { return _lastModifiedDate; } }

        // <summary>
        // The first date this Video is available to be played
        // </summary> 
        public DateTime startDate { get { return _startDate; } }

        // <summary>
        // The last date this Video is available to be played
        // </summary> 
        public DateTime endDate { get { return _endDate; } }

        // <summary>
        // An optional URL to a related item.
        // </summary> 
        public String linkURL { get; set; }

        // <summary>
        // The text displayed for the linkURL.
        // </summary> 
        public String linkText { get; set; }

        // <summary>
        // A list of Strings representing the tags assigned to this Video.
        // </summary> 
        public List<String> tags = new List<string>();

        // <summary>
        // The URL to the video still image associated with this Video. Video stills are 480x360 pixels.
        // </summary> 
        public String videoStillURL { get; set; }

        // <summary>
        // The URL to the thumbnail image associated with this Video. Thumbnails are 120x90 pixels.
        // </summary> 
        public String thumbnailURL { get; set; }

        // <summary>
        // The length of this video in milliseconds.
        // </summary> 
        public long length { get { return _length; } }

        // <summary>
        // Either FREE or AD_SUPPORTED. AD_SUPPORTED means that ad requests are enabled for this Video.
        // </summary> 
        public BCVideoEconomics economics { get; set; }

        // <summary>
        // A List of the CuePoints objects assigned to this Video.
        // </summary> 
        public List<BCCuePoint> cuePoints = new List<BCCuePoint>();

        // <summary>
        // How many times this Video has been played since its creation.
        // </summary> 
        public int playsTotal { get { return _playsTotal; } }

        // <summary>
        // How many times this Video has been played within the past seven days, exclusive of today.
        // </summary> 
        public int playsTrailingWeek { get { return _playsTrailingWeek; } }

        #endregion Public Properties

        #region Constructor

        // <summary>
        // Constructor, getting a JSON Reader passed for initialization
        // </summary> 
        public BCVideo(JsonReader reader)
        {
            reader.Read();
            while (reader.TokenType != JsonToken.EndObject)
            {
                if (reader.TokenType == JsonToken.PropertyName)
                {

                    switch (reader.Value.ToString())
                    {
                        case "error":
                            reader.Read();
                            Console.WriteLine(String.Format("Error: {0}", reader.Value));
                            break;

                        case "name":
                            reader.Read();
                            name = (string)reader.Value;
                            break;

                        case "id":
                            reader.Read();
                            _id = (long)reader.Value;
                            break;

                        case "referenceId":
                            reader.Read();
                            referenceId = (string)reader.Value;
                            break;

                        case "accountId":
                            reader.Read();
                            _accountId = (long)reader.Value;
                            break;

                        case "shortDescription":
                            reader.Read();
                            shortDescription = (string)reader.Value;
                            break;

                        case "longDescription":
                            reader.Read();
                            longDescription = (string)reader.Value;
                            break;

                        case "FLVURL":
                            reader.Read();
                            _FLVURL = (string)reader.Value;
                            break;

                        //Not yet implemented
                        case "renditions":
                            reader.Read();
                            Console.WriteLine(reader.Value.ToString());
                            break;

                        case "creationDate":
                            reader.Read();
                            _creationDate = DateFromUnix(reader.Value);
                            break;

                        case "publishedDate":
                            reader.Read();
                            _publishedDate = DateFromUnix(reader.Value);
                            break;

                        case "lastModifiedDate":
                            reader.Read();
                            _lastModifiedDate = DateFromUnix(reader.Value);
                            break;

                        case "startDate":
                            reader.Read();
                            _startDate = DateFromUnix(reader.Value);
                            break;

                        case "endDate":
                            reader.Read();
                            _endDate = DateFromUnix(reader.Value);
                            break;

                        case "linkURL":
                            reader.Read();
                            linkURL = (string)reader.Value;
                            break;

                        case "linkText":
                            reader.Read();
                            linkText = (string)reader.Value;
                            break;

                        case "tags":
                            reader.Read();
                            tags.Clear();
                            if (reader.Value != null)
                            {
                                tags = ExplodeString((string)reader.Value);
                            }
                            break;

                        case "videoStillURL":
                            reader.Read();
                            videoStillURL = (string)reader.Value;
                            break;

                        case "thumbnailURL":
                            reader.Read();
                            thumbnailURL = (string)reader.Value;
                            break;

                        case "length":
                            reader.Read();
                            _length = (long)reader.Value;
                            break;

                        case "economics":
                            reader.Read();
                            economics = (BCVideoEconomics)Enum.Parse(typeof(BCVideoEconomics), reader.Value.ToString());
                            break;

                        //not yet implemented
                        case "cuePoints":
                            reader.Read();
                            //
                            break;

                        case "playsTotal":
                            reader.Read();
                            _playsTotal = int.Parse(reader.Value.ToString());
                            break;                       

                        case "playsTrailingWeek":
                            reader.Read();
                            _playsTrailingWeek = int.Parse(reader.Value.ToString());
                            break;

                        default:
                            reader.Read();
                            break;
                    }
                }
                reader.Read();
            }
            reader.Read();
        }

        #endregion Constructor
    }
}
